package com.wxsong.authplatform.authweb.util;

import org.apache.shiro.authc.credential.DefaultPasswordService;
import org.apache.shiro.crypto.SecureRandomNumberGenerator;
import org.apache.shiro.crypto.hash.DefaultHashService;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.util.SimpleByteSource;

public class PasswordUtil {

    /**
     * 加密
     * @param str
     * @return
     */
    public static String encry(String str){
        String salt = "";
        DefaultPasswordService passwordService = new DefaultPasswordService();
        DefaultHashService hashService = new DefaultHashService();
        hashService.setHashAlgorithmName(Md5Hash.ALGORITHM_NAME);
        hashService.setPrivateSalt(new SimpleByteSource(salt));         //私盐
        hashService.setGeneratePublicSalt(true);                        //是否生成公盐
        hashService.setRandomNumberGenerator(new SecureRandomNumberGenerator());    //用于生成公盐
        hashService.setHashIterations(2);
        passwordService.setHashService(hashService);

        return passwordService.encryptPassword(str);
    }

    public static void main(String[] args) {
        String enc = encry("111111");
        System.out.println(enc);
    }

}
